package com.mud.dnd.ejb;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionManagement;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.domain.common.QueryParameter;
import com.mud.dnd.domain.dao.DnDDAO;
import com.mud.dnd.domain.model.Action;
import com.mud.dnd.domain.model.Card;
import com.mud.dnd.domain.model.Deck;
import com.mud.dnd.domain.model.User;
import com.mud.dnd.ejb.common.AuthenticationException;
import com.mud.dnd.ejb.services.AuthenticationService;


@Stateless
@TransactionManagement
public class CardManager {
	
	@EJB
	private DnDDAO dao;
	
	@EJB
	private AuthenticationService userService;
	
	@TransactionAttribute
	public List<Card> getCards(List<QueryParameter> params) {
		
		List<Card> cards= dao.getCards(params);
		
		for(Card card : cards)
		{
			List<Integer> deckIDs = new ArrayList<Integer>();
			List<Integer> actionIDs = new ArrayList<Integer>();
			
			for(Deck deck :card.getDecks() )
			{
				deckIDs.add(deck.getId());
			}
			
		
			card.setDeckIDs(deckIDs);
		}

		return cards;
	}

	@TransactionAttribute
	public int createCard(String name, String description, String pictureURL,
			String type, int rarity, String userName, String sessionID) throws DAOException, AuthenticationException {
		
		
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		
		User author = userService.getUserData(userName);
		
		return dao.createCard(name, description, pictureURL, type, rarity,author);

	}
	
	@TransactionAttribute
	public void addCardtoDeck(int cardID, int deckID, String userName, String sessionID) throws DAOException, AuthenticationException
	{
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		if(!userService.checkIfUserIsOwner(userName, deckID))
		{
			throw new AuthenticationException("You can edit only your deck.");
			
		}
		
		
		dao.addCardtoDeck(cardID, deckID);
		
	}
	
	@TransactionAttribute
	public void updateCard(int id, String name, String description,
			String pictureURL, String type, int rate, int rarity, String userName, String sessionID) throws DAOException, AuthenticationException
	{
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		if(!userService.checkIfUserIsOwner(userName, id) && !( name==null && description==null && pictureURL==null && type ==null && rate!=0 && rarity==0))
		{
			throw new AuthenticationException("You can edit only your card.");
			
		}
		
		
		dao.updateCard(id, name, description, pictureURL, null, type, rate, rarity);
	}
	
	@TransactionAttribute
	public void removeCardfromDeck(int cardID, int deckID , String userName, String sessionID) throws DAOException, AuthenticationException
	{
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		if(!userService.checkIfUserIsOwner(userName, deckID))
		{
			throw new AuthenticationException("You can edit only your deck.");
			
		}
		
		
		dao.removeCardfromDeck(cardID, deckID);
	}
	
	@TransactionAttribute
	public void deleteCard(int cardID , String userName, String sessionID) throws DAOException, AuthenticationException
	{
		
		if(!userService.checkSessionForUser(userName, sessionID))
		{
			throw new AuthenticationException("Login required.");
		}
		if(!userService.checkIfUserIsOwner(userName, cardID))
		{
			throw new AuthenticationException("You can edit only your card.");
			
		}
		
		
		dao.deleteCard(cardID);
		
	}
	
	

}
